//
//  TrackRecord1TableViewController.m
//  PlayerWorld
//
//  Created by 1 on 16/5/12.
//  Copyright © 2016年 刘成. All rights reserved.
//

#import "TrackRecord1TableViewController.h"
//#import "TrackRecordTableViewCell.h"
#import "INeedToCatchUpViewController.h"
#import "PageCell1TableViewCell.h"
#import "BettingDetailsViewController.h"
#import "G_P_Object.h"
#import "AFNetworking.h"
#import "BettingRecordModel.h"
#import "ProcurementHallModel.h"

@interface TrackRecord1TableViewController ()

@end

@implementation TrackRecord1TableViewController
{
    NSMutableArray *_pageCell1Array;
    
    NSMutableArray *_nameArray;
    NSMutableArray *_listArray;
    NSInteger _index;
}
-(void)obtainG_N_Data
{
    // 启动系统风火轮
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    //服务器给的域名
    NSString *domainStr = @"http://666.lefun6.com/";
    
    //创建一个可变字典
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    //往字典里面添加需要提交的参数
    [parametersDic setObject:@"System" forKey:@"Model"];
    [parametersDic setObject:@"GetSystemConfig" forKey:@"Action"];
    [parametersDic setObject:@"HX_Game_Name" forKey:@"models"];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //以post的形式提交，POST的参数就是上面的域名，parameters的参数是一个字典类型，将上面的字典作为它的参数
    [manager POST:domainStr parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 解析成功隐藏系统风火轮
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        _listArray = [NSMutableArray array];
        for (G_P_Object *model1 in _nameArray) {
            for (NSDictionary *modeldic in [dic objectForKey:@"HX_Game_Name"]) {
                ProcurementHallModel *model = [[ProcurementHallModel alloc]init];
                [model setValuesForKeysWithDictionary:modeldic];
                if ([model1.G_P_NameId isEqualToString:model.G_N_Id]) {
                    [_listArray addObject:model];
                }
            }
        }
        [self.tableView reloadData];
//        [self.tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        // 解析失败隐藏系统风火轮(可以打印error.userInfo查看错误信息)
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
    }];
    
}

-(void)obtainG_PData
{
    // 启动系统风火轮
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    //服务器给的域名
    NSString *domainStr = @"http://666.lefun6.com/";
    
    //创建一个可变字典
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    //往字典里面添加需要提交的参数
    [parametersDic setObject:@"System" forKey:@"Model"];
    [parametersDic setObject:@"GetSystemConfig" forKey:@"Action"];
    [parametersDic setObject:@"HX_Game_Play" forKey:@"models"];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //以post的形式提交，POST的参数就是上面的域名，parameters的参数是一个字典类型，将上面的字典作为它的参数
    [manager POST:domainStr parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 解析成功隐藏系统风火轮
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        _nameArray = [NSMutableArray array];
        for (BettingRecordModel *model1 in _pageCell1Array) {
            for (NSDictionary *dictionary in [dic objectForKey:@"HX_Game_Play"]) {
                G_P_Object *model = [[G_P_Object alloc] init];
                [model setValuesForKeysWithDictionary:dictionary];
                if ([model1.B_L_Type isEqualToString:model.G_P_Id]) {
                    [_nameArray addObject:model];
                }
            }
        }
        [self obtainG_N_Data];
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        // 解析失败隐藏系统风火轮(可以打印error.userInfo查看错误信息)
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
    }];
    
}
-(void)obtainData
{
    // 启动系统风火轮
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    //服务器给的域名
    NSString *domainStr = @"http://666.lefun6.com/";
    
    //创建一个可变字典
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    //往字典里面添加需要提交的参数
    [parametersDic setObject:@"User" forKey:@"Model"];
    [parametersDic setObject:@"BettingRecords_List" forKey:@"Action"];
    [parametersDic setObject:@"1" forKey:@"type4"];
    [parametersDic setObject:@"1000" forKey:@"pagenum"];
    [parametersDic setObject:@"1" forKey:@"showpagenum"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //以post的形式提交，POST的参数就是上面的域名，parameters的参数是一个字典类型，将上面的字典作为它的参数
    [manager POST:domainStr parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 解析成功隐藏系统风火轮
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
//        NSLog(@"%@",dic);
        _pageCell1Array = [[NSMutableArray alloc]init];
        
        for (NSDictionary *modeldic in [dic objectForKey:@"msg"]) {
            BettingRecordModel *model = [[BettingRecordModel alloc]init];
            [model setValuesForKeysWithDictionary:modeldic];
            [_pageCell1Array addObject:model];
            //            [self obtainData2WithId:model.B_L_Id];
        }
        [self obtainG_PData];
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        // 解析失败隐藏系统风火轮(可以打印error.userInfo查看错误信息)
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
    }];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self obtainData];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listArray.count;
}

- (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

//设置单元格中的数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdent = @"PageCell1TableViewCell";
    PageCell1TableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:cellIdent];
    if (cell == nil) {
        cell = [[PageCell1TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdent];
    }
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    BettingRecordModel *model = [_pageCell1Array objectAtIndex:indexPath.row];
    G_P_Object *g_p_model = _nameArray[indexPath.row];
    ProcurementHallModel *g_n_model = _listArray[indexPath.row];
    //    cell.bettingRecordLabel.text = [_nameArray objectAtIndex:indexPath.row];
    cell.bettingRecordLabel.text = [NSString stringWithFormat:@"%@-%@",g_n_model.G_N_Title,g_p_model.G_P_Name];
    cell.bettingissueLabel.text = [NSString stringWithFormat:@"%@期",model.B_L_Issue];
    cell.BettingtimeLabel.text = model.B_L_AddTime;
    cell.betMoneyLabel.text = model.B_L_Money;
    if ([model.B_L_State isEqualToString:@"3"]) {
        cell.bettingResultsLabel.text = @"未中奖";
    }
    if ([model.B_L_State isEqualToString:@"2"]) {
        cell.bettingResultsLabel.text = @"已中奖";
    }
    if ([model.B_L_State isEqualToString:@"1"]) {
        cell.bettingResultsLabel.text = @"未开奖";
    }
    if ([model.B_L_State isEqualToString:@"4"]) {
        cell.bettingResultsLabel.text = @"派奖中";
    }
    if ([model.B_L_State isEqualToString:@"5"]) {
        cell.bettingResultsLabel.text = @"已撤单";
    }
    if ([model.B_L_State isEqualToString:@"6"]) {
        cell.bettingResultsLabel.text = @"完成";
    }
    return cell;
}
//单元格点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    BettingDetailsViewController *bDVC= [[BettingDetailsViewController alloc] init];
    bDVC.bettingRecordModel = [_pageCell1Array objectAtIndex:indexPath.row];
    bDVC.G_N_Model = [_listArray objectAtIndex:indexPath.row];
    bDVC.G_P_Model = [_nameArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:bDVC animated:YES];
    
}
////单元格点击方法
//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    
//    INeedToCatchUpViewController *iNTCUVC= [[INeedToCatchUpViewController alloc] init];
//    [self.navigationController pushViewController:iNTCUVC animated:YES];
//    
//}


@end
